package com.sinaapp.kltq.dinosaurweather;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.weathermanager.WeatherManager;


public class AddCityActivity extends Activity {

    private String currentSearch;
    private String currentResult;

    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ((TextView) findViewById(R.id.searchResult)).setText(currentResult); // 显示
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_add_city);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar_search_city);

        ((Button) findViewById(R.id.searchCancelBtn)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

        currentResult = "无数据";

        ((TextView) findViewById(R.id.cityBeijing)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("北京");
            }
        });
        ((TextView) findViewById(R.id.cityShanghai)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("上海");
            }
        });
        ((TextView) findViewById(R.id.cityTianjin)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("天津");
            }
        });
        ((TextView) findViewById(R.id.cityHangzhou)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("杭州");
            }
        });
        ((TextView) findViewById(R.id.cityNanjing)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("南京");
            }
        });
        ((TextView) findViewById(R.id.cityJiangshan)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("江山");
            }
        });
        ((TextView) findViewById(R.id.cityJinzhou)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity("锦州");
            }
        });

        ((EditText) findViewById(R.id.searchText)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                currentSearch = s.toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        WeatherManager wm = new WeatherManager(getApplicationContext());
                        if (wm.checkCity(currentSearch.toString())) {   // 如果城市存在
                            currentResult = currentSearch.toString();   // 修改当前结果
                        } else {
                            currentResult = "无数据";    // 修改当期结果
                        }
                        Message message = new Message();
                        message.what = 1;
                        myHandler.sendMessage(message);
                    }
                }).start();
            }
        });
        ((TextView) findViewById(R.id.searchResult)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentResult.compareTo("无数据") != 0) {
                    addCity(currentResult);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        back();
        super.onBackPressed();
    }

    private void addCity(String city) {
        WeatherManager wm = new WeatherManager(getApplicationContext());
        boolean exist = false;
        for (CityListItem item : wm.getCityList()) {
            exist = (item.city.compareTo(city) == 0) ? true : exist;
        }
        if (exist) { // 如果城市已经在列表中
            Toast.makeText(getApplicationContext(), "城市已存在", Toast.LENGTH_SHORT).show(); // 提示城市已存在
        } else {    // 如果城市不在列表中
            wm.addCityIntoListEnd(city);    // 添加城市
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 接口
                    WeatherManager wm = new WeatherManager(getApplicationContext());
                    wm.pleaseUpdateThisSir(wm.getCityList().get(wm.getCityList().size() - 1).city);
                }
            }).start();
            Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show(); // 提示添加成功
        }
    }

    private void back() {
        Intent intent = new Intent(AddCityActivity.this, CityManagement.class);
        intent.putExtra("cityIndex", getIntent().getIntExtra("cityIndex", 0));
        intent.putExtra("pageType", getIntent().getCharExtra("pageType", 'r'));
        startActivity(intent);
        overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
        finish();
    }

}
